/*
 * Copyright (C) 2014 Eduardo Pereira Montenegro <epereiramontenegro@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.bambole.facade;

import com.googlecode.bambole.domain.Logo;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Eduardo Pereira Montenegro <epereiramontenegro@gmail.com>
 */
@Stateless
public class LogoFacade extends AbstractFacade<Logo> {

    @PersistenceContext(unitName = "BambolePU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public LogoFacade() {
        super(Logo.class);
    }

    public boolean isEnabledLogoUnique(int id) {

        List<Logo> logoList;

        Query query = em.createNamedQuery("Logo.findEnabled");
        logoList = query.getResultList();

        return logoList.isEmpty() || (logoList.size() == 1 && logoList.get(0).getId() == id);
    }

    public byte[] getDefaultLogo() {

        List<Logo> logoList;

        Query query = em.createNamedQuery("Logo.findEnabled");
        logoList = query.getResultList();

        byte[] logo = new byte[0];

        if (!logoList.isEmpty()) {
            logo = logoList.get(0).getLogo();
        }

        return logo;
    }
}
